<?php

namespace App\Admin\Controllers;


use App\Models\FhPage;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FhPageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhPage(), function (Grid $grid) {
            $grid->name->tree(true); // 开启树状表格功能 
            $grid->sort->orderable();
            $grid->column('url')->width(300);
            $grid->column('banner','背景图片')->image("",100,40)->width(150);
            $grid->column('url_type')->using([1=>"站内导航",2=>"其他网站",3=>"单页面内容"]);
            $grid->column('p_type')->using([1=>"主导航",2=>"次级导航"]);
            $grid->column('p_id')->display(function ($p_id) {
                $title = FhPage::where('id', $p_id)->value('name');
                return $title==""?"顶级":$title;
            });
            $grid->column('sort')->editable();
            $grid->column('show')->switch();
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhPage(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('url');
            $show->field('url_type');
            $show->field('p_type');
            $show->field('p_id');
            $show->field('sort');
            $show->field('show');
            $show->field('htmls');
            // $show->field('keyword');
            // $show->field('description');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FhPage(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('url')->required()->help("路由地址请勿输入?&.{}空格特殊字符,请把上述字符替换成减号“-”");
            $form->image('banner','背景图片')->dimensions(['max_width' => 1200, 'max_heigth' => 380])->required()->move('images/'.date("Ymd",time()))->help("最佳分辨率 1200*370");
            $form->radio('url_type')->when(3,function(Form $form){
                $form->editor("htmls","页面内容");
            })->options([1=>"站内导航",2=>"其他网站",3=>"单页面内容"])->default(1);
            $form->radio('p_type')->when(1,function(Form $form){
                $form->text("desc","导航栏简单描述");
                $form->image("img","导航栏展示图片")->widen(300);
            })->options([1=>"主导航",2=>"次级导航"])->default(1);
            $form->select('p_id')->options(FhPage::selectOptions())->default(0);
            $form->number('sort')->min(1);
            $form->switch('show')->default(1);
            $form->text('keyword');
            $form->text('description');
             $form->saving(function (Form $form) {
                $ids = $form->model()->id; 
                if (!$form->model()->id) {
                    $pattern = '/[?\&.\{\}\s]/';  // 正则表达式
                    $isValid = preg_match($pattern,  $form->url) === 1; // 验证逻辑
                    if($isValid){
                        throw new \Exception('路由地址包含?&.{}# 空格特殊字符,请把上述字符替换成减号“-” ');
                    }
                }
                
            });
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
